package com.example.automatic.abs_read;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import com.example.automatic.utils.LogUtils;
import com.example.automatic.utils.RandomUtils;

import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MDAccessibilityService extends AccessibilityService {

    private boolean isReaderActivity = false;

    private Handler mHandler = new Handler();

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            aboutGesture();
            long delay = RandomUtils.getNum(10, 15) * 100;
            mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        LogUtils.e("开启米读...");
    }

    @Override
    public void onAccessibilityEvent(final AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (event.getClassName().equals("com.lechuan.midunovel.reader.ui.activity.ReaderActivity")) {
                if (!isReaderActivity)
                    mHandler.postDelayed(mRunnable, 5000);
                isReaderActivity = true;
            }
        }

    }

    @Override
    public void onInterrupt() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRunnable);
        isReaderActivity = false;
        LogUtils.e("关闭米读...");
    }

    /**
     * 获取屏幕宽高
     */
    private DisplayMetrics getDisplayMetrics() {
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) Objects.requireNonNull(getSystemService(WINDOW_SERVICE)))
                .getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    /**
     * 滑动 右→左
     */
    public void aboutGesture(AccessibilityNodeInfo nodeInfo) {
        int width, height;
        if (nodeInfo == null) {
            width = getDisplayMetrics().widthPixels;
            height = getDisplayMetrics().heightPixels;
        } else {
            Rect rect = new Rect();
            nodeInfo.getBoundsInScreen(rect);
            width = rect.right;
            height = rect.bottom;
        }
        Path path = new Path();
//        path.moveTo(RandomUtils.getNum(width - (width * 3 / 8), width - (width / 8)),
//                RandomUtils.getNum(height / 8, height / 4));
//        path.lineTo(RandomUtils.getNum(width / 4, width / 2),
//                RandomUtils.getNum(height / 8, height / 4));

//        path.moveTo(width - (width / 10), height / 10);
//        path.lineTo(width / 10, height / 10);

        path.moveTo(width - 10, height / 10);
        path.lineTo(width / 8, height / 10);

        dispatchGesture(path);
    }

    public void aboutGesture() {
        this.aboutGesture(null);
    }

    /**
     * 滑动 下→上
     */
    public void updownGesture(AccessibilityNodeInfo nodeInfo) {
        int width, height;
        if (nodeInfo == null) {
            width = getDisplayMetrics().widthPixels;
            height = getDisplayMetrics().heightPixels;
        } else {
            Rect rect = new Rect();
            nodeInfo.getBoundsInScreen(rect);
            width = rect.right;
            height = rect.bottom;
        }
        Path path = new Path();
        path.moveTo(RandomUtils.getNum(width - (width * 3 / 8), width - (width / 4)),
                RandomUtils.getNum(height - (height / 2), height - (height / 4)));
        path.lineTo(RandomUtils.getNum(width - (width / 4), width / 2),
                RandomUtils.getNum(height / 4, height / 2));
        dispatchGesture(path);
    }

    /**
     * 滑动
     */
    private void dispatchGesture(Path path) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            GestureDescription.Builder builder = new GestureDescription.Builder();
            GestureDescription gestureDescription = builder
                    .addStroke(new GestureDescription.StrokeDescription(path, 0L, 20L))
                    .build();
            dispatchGesture(gestureDescription, null, null);
        } else {
            performGlobalAction(GESTURE_SWIPE_LEFT);
        }
    }

}
